package com.yu1996.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yu1996.constant.MessageConstant;
import com.yu1996.constant.RedisConstant;
import com.yu1996.entity.PageResult;
import com.yu1996.entity.QueryPageBean;
import com.yu1996.entity.Result;
import com.yu1996.pojo.Setmeal;
import com.yu1996.service.SetmealService;
import com.yu1996.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.UUID;


/**
 * @Description: 套餐类controller
 * @author: xing
 * @Date: 2021/7/2 22:07
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;

    @Autowired
    private JedisPool jedisPool;  //redis

    /**
     * @Description 文件上传
     * @Author xing
     * @Date 2021/7/2 15:22
     * @Param 文件封装类
     * @Return 返回数据的封装类
     */
    @PostMapping("/upload")
    public Result findAll(MultipartFile imgFile) {
        Result result = null;
        String originalFilename = imgFile.getOriginalFilename();//获取原始文件名
        int lastIndexOf = originalFilename.lastIndexOf(".");
        String suffix = originalFilename.substring(lastIndexOf);//获取文件后缀
//        System.out.println("存储文件后缀：" + suffix);
        //使用UUID随机产生文件名称，防止同名文件覆盖
        String fileName = UUID.randomUUID().toString() + suffix;
        try {
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);
            //文件上传后将其保存到Redis 基于Redis的Set集合存储
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);
            result = new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, fileName);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            //图片上传失败
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }


    /**
     * @Description 添加套餐
     * @Author xing
     * @Date 2021/7/2 22:08
     * @Param 套餐基本信息、检查组的id数组
     * @Return 返回数据的封装类
     */
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        try {
            setmealService.add(setmeal, checkgroupIds);
        } catch (Exception e) { //新增套餐失败
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }//新增套餐成功
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * @Description 分页查询套餐
     * @Author xing
     * @Date 2021/7/3 15:08
     * @Param 当前页、每页显示条数、查询条件
     * @Return 封装的分页查询结果集
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = setmealService.pageQuery(
                queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
        return pageResult;
    }
}

